<template>
  <div class="data-management-container">
    <!-- 主表格 -->
    <el-table :data="tableData" border style="width: 100%" class="main-table">
      <el-table-column prop="name" label="名称" width="120"></el-table-column>
      <el-table-column prop="desc" label="介绍名称"></el-table-column>
      <el-table-column prop="tag" label="标签"></el-table-column>
      <el-table-column prop="type" label="优惠卷类型">
        <template slot-scope="scope">
          {{scope.row.type==0 ? "通用领卷":scope.row.type==1 ? "注册赠券":"优惠券码兑换"}}
        </template>
      </el-table-column>
      <el-table-column prop="min" label="最低消费"></el-table-column>
      <el-table-column prop="discount" label="优惠面值"></el-table-column>
    </el-table>

    <!-- 状态表格 -->
    <el-table :data="tableData" border style="width: 100%" class="status-table">
      <el-table-column prop="limit" label="每人限额" width="120">
        <template slot-scope="scope">
          {{scope.row.limit == 0 ? "不限" : "限领一张"}}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="当前状态">
        <template slot-scope="scope">
          {{scope.row.status==0 ? "正常":scope.row.status==1 ? "过期":"下架"}}
        </template>
      </el-table-column>
      <el-table-column prop="desc" label="商品范围"></el-table-column>
      <el-table-column prop="timeType" label="有效期">
        <template slot-scope="scope">
          {{scope.row.timeType == 0 ? scope.row.days : scope.row.startTime+"-"+scope.row.endTime}}
        </template>
      </el-table-column>
      <el-table-column prop="code" label="优惠兑换码"></el-table-column>
      <el-table-column prop="total" label="发行数量">
        <template slot-scope="scope">
          {{scope.row.total == 0 ? "不限" : scope.row.total}}
        </template>
      </el-table-column>
    </el-table>

    <!-- 用户信息部分 -->
    <div class="user-section">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="用户Id" prop="userId">
          <el-input
            v-model="queryParams.userId"
            placeholder="请输入用户Id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="使用状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择使用状态" >
            <el-option
              v-for="isho in positions"
              :key="isho.id"
              :label="isho.value"
              :value="isho.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="userData" border style="width: 100%">
        <el-table-column prop="id" label="用户优惠卷ID" width="120"></el-table-column>
        <el-table-column prop="userId" label="用户ID" width="120"></el-table-column>
        <el-table-column prop="addTime" label="领取时间"></el-table-column>
        <el-table-column prop="status" label="使用状态">
          <template slot-scope="scope">
            {{scope.row.status==0 ? "未使用":scope.row.status==1 ? "已使用":scope.row.status==2 ? "已过期":"已下架"}}
          </template>
        </el-table-column>
        <el-table-column prop="orderSn" label="订单ID"></el-table-column>
        <el-table-column prop="usedTime" label="使用时间"></el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="CouponUserList"
      />
    </div>
  </div>
</template>

<script>
  import { listCouponUser,getCoupon} from "@/api/popularize/coupon";

export default {
  name: 'DataManagement',
  data() {
    return {
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        couponId : this.$route.query.id,
        userId: null,
        status: null,
      },
      positions: [
        { id : 0 , value: '未使用'},
        { id : 1 , value: '已使用'},
        { id : 2 , value: '已过期'},
        { id : 3 , value: '已下架'}
      ],
      tableData: [],
      userData: []
    };
  },
  created(){
  this.CouponList();
  this.CouponUserList();
  },
  methods:{
  CouponList(){
    const id=this.$route.query.id;
    //查询优惠卷详情
    getCoupon(id).then(response => {
      this.tableData = Array.isArray(response.data) ? response.data : [response.data];
      console.log(response.data);
    });
      console.log(this.queryParams);

  },
  CouponUserList(){
    //查询优惠卷用户表
    listCouponUser(this.queryParams).then(response => {
      this.userData = response.rows;
      this.total = response.total;
      console.log(this.userData);
      console.log(response.rows);
    });
  },
  handleQuery(){
    this.CouponUserList();
  }
  }
}
</script>

<style scoped>
.data-management-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.main-table, .status-table {
  margin-bottom: 30px;
}

.section-title {
  font-size: 18px;
  color: #333;
  margin: 25px 0 15px;
}

.sub-title {
  font-size: 16px;
  color: #666;
  margin: 20px 0 10px;
}

.status-normal {
  color: #67C23A;
  font-weight: bold;
}

.el-divider {
  margin: 25px 0;
}
</style>
